package com.itheima.tliasmanagementspringboot.controller;

import com.itheima.tliasmanagementspringboot.common.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;


@Slf4j
@RestController
public class SessionController {
    @GetMapping("/s1")
    public R session1(HttpSession session){
        log.info("HttpSession-s1: {}", session.hashCode());
        //(同一会话)连续多次访问(s1),不会多次生成Session
        //客户端关闭会话(浏览器),再访问会再次生成新的Session会话对象和ID
        session.setAttribute("loginUser", "tom"); //往session中存储数据
        return R.ok();
    }

    @GetMapping("/s2")
    public R session2(HttpServletRequest request){
        HttpSession session = request.getSession();
        log.info("HttpSession-s2: {}", session.hashCode());

        Object loginUser = session.getAttribute("loginUser"); //从session中获取数据
        log.info("loginUser: {}", loginUser);
        return R.ok(loginUser);
    }

}
